package com.qst.itoffer.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qst.itoffer.bean.Applicant;
import com.qst.itoffer.bean.JobApply;
import com.qst.itoffer.dao.JobApplyDao;


@SuppressWarnings("ALL")
@WebServlet("/JobApplyServlet")
public class JobApplyServlet extends HttpServlet {

    public JobApplyServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        // 获取操作类型
        String type = request.getParameter("type");

        if ("apply".equals(type)) {
            // 获取职位编号
            String jobid = request.getParameter("jobid");
            // 获取登录用户
            Applicant applicant = (Applicant) request.getSession()
                    .getAttribute("SESSION_APPLICANT");
            // 添加此用户对此职位的申请
            JobApplyDao dao = new JobApplyDao();
            dao.save(jobid, applicant.getApplicantId());
            response.sendRedirect("JobApplyServlet?type=myapply");

        } else if ("myapply".equals(type)) {
            // 获取登录用户
            Applicant applicant = (Applicant) request.getSession()
                    .getAttribute("SESSION_APPLICANT");
            // 根据用户标识查询此用户申请的所有职位
            JobApplyDao dao = new JobApplyDao();
            List<JobApply> jobList = dao.getJobApplyList(applicant
                    .getApplicantId());
            request.setAttribute("jobList", jobList);
            request.getRequestDispatcher("applicant/jobApply.jsp").forward(
                    request, response);
        }
    }

}
